package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.GroupType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.GroupInfo;
import com.smfx.yxt.model.StudentRoutineParam;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.PointsReqVO;
import com.smfx.yxt.reqVo.StudentRoutineReqVO;
import com.smfx.yxt.service.IClassPointService;
import com.smfx.yxt.service.IGroupInfoService;
import com.smfx.yxt.service.IStudentPointService;
import com.smfx.yxt.service.IStudentRoutineParamService;
import com.smfx.yxt.service.IStudentRoutineService;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.StudentRoutineView;
import com.smfx.yxt.vo.PointsQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.STUDENT_POINTS)
@Controller
public class StudentPointsController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(StudentPointsController.class);
	
	@Autowired
	private IStudentRoutineParamService studentRoutineParamService;
	@Autowired
	private IStudentRoutineService studentRoutineService;
	@Autowired
	private IGroupInfoService groupInfoService;
	@Autowired
	private IStudentPointService studentPointService;
	@Autowired
	private IClassPointService classPointService;

	@RequestMapping(value = {"","/"})
	public String index(Model model) {
		AppUser appUser = getAppUser();
		if(StringUtil.isExistPosition(appUser.getTeacher().getPosition(), "5")) {
			model.addAttribute("isAdmin", true);
		}
		model.addAttribute("isJudgeTeacher", studentRoutineParamService.
				isJudgeTeacher(appUser.getTeacher().getId()));
		return "studentPoints/index";
	}
	
	/**
	 * 学生常规登记
	 */
	@RequestMapping("register")
	public String register() {
		return "studentPoints/register";
	}
	
	/**
	 * 类别列表
	 * @return
	 */
	@RequestMapping("typeList")
	public String typeList(Model model) {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		List<StudentRoutineParam> list = studentRoutineParamService.selectList(vo);
		model.addAttribute("list", list);
		return "studentPoints/typeList";
	}
	/**
	 * 类别详情
	 * @param model
	 * @param typeId
	 * @return
	 */
	@RequestMapping("type/detail/{typeId}")
	public String typeDetail(Model model, @PathVariable Integer typeId) {
		model.addAttribute("typeId", typeId);
		return "studentPoints/typeDetail";
	}
	
	/**
	 * 值班组列表
	 * @return
	 */
	@RequestMapping("groupList")
	public String groupList(Model model) {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		vo.setType(GroupType.ON_DUTY.getCode());
		List<GroupInfo> list = groupInfoService.selectList(vo);
		model.addAttribute("list", list);
		return "studentPoints/groupList";
	}
	
	/**
	 * 值班组详情
	 * @param model
	 * @param typeId
	 * @return
	 */
	@RequestMapping("group/detail/{groupId}")
	public String groupDetail(Model model, @PathVariable Integer groupId) {
		model.addAttribute("groupId", groupId);
		return "studentPoints/groupDetail";
	}
	
	@RequestMapping("routine/detail/{id}")
	public String routineDetail(Model model, @PathVariable Integer id){
		model.addAttribute("detail", studentRoutineService.selectViewById(id));
		return "studentPoints/routineDetail";
	}
	
	@RequestMapping("student")
	public String personalPoints() {
		return "studentPoints/studentPoint";
	}
	
	@RequestMapping("class")
	public String schoolPoints() {
		return "studentPoints/classPoint";
	}
	
	/**
	 * 类别详情
	 * @param typeId
	 * @return
	 */
	@RequestMapping("type/detail/query")
	@ResponseBody Object typeDetailQuery(Integer typeId) {
		return studentRoutineParamService.selectById(typeId);
	}
	
	/**
	 * 值班组详情
	 * @param groupId
	 * @return
	 */
	@RequestMapping("group/detail/query")
	@ResponseBody Object groupDetailQuery(Integer groupId) {
		return groupInfoService.selectByPrimaryKey(groupId);
	}
	
	/**
	 * 类别修改
	 * @param record
	 * @return
	 */
	@RequestMapping("type/addUpdate")
	@ResponseBody Object typeUpdate(StudentRoutineParam record) {
		try {
			AppUser appUser = getAppUser();
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			studentRoutineParamService.addUpdate(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---typeUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 类别修改
	 * @param record
	 * @return
	 */
	@RequestMapping("group/addUpdate")
	@ResponseBody Object groupUpdate(GroupInfo record) {
		try {
			AppUser appUser = getAppUser();
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			record.setGroupType(GroupType.ON_DUTY.getCode());
			groupInfoService.addUpdate(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---typeUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("register/power")
	@ResponseBody Object registerPower() {
		AppUser appUser = getAppUser();
		return studentRoutineParamService.selectByTeacherId(appUser.getTeacher().getId());
	}
	
	/**
	 * 学生常规登记提交
	 * @param vo
	 * @return
	 */
	@RequestMapping("register/submit")
	@ResponseBody Object registerSubmit(StudentRoutineReqVO vo) {
		try {
			AppUser appUser = getAppUser();
			vo.setJudgeTeacherId(appUser.getTeacher().getId());
			vo.setTermId(getCurrentTerm().getId());
			vo.setSchoolId(appUser.getTeacher().getSchoolId());
			studentRoutineService.addStudentRoutine(vo);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--registerSubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("group/list/query")
	@ResponseBody Object groupListQuery() {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setSchoolId(appUser.getTeacher().getSchoolId());
		vo.setType(GroupType.ON_DUTY.getCode());
		return groupInfoService.selectList(vo);
	}
	
	@RequestMapping("totalPoint")
	@ResponseBody Object totalPoint(Integer keyId) {
		PointsReqVO vo = new PointsReqVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setKeyId(keyId);
		return studentPointService.selectTotalPointForStudent(vo);
	}
	
	@RequestMapping("class/totalPoint")
	@ResponseBody Object classTotalPoint(Integer keyId) {
		PointsReqVO vo = new PointsReqVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setKeyId(keyId);
		return classPointService.selectTotalPointForClass(vo);
	}
	
	@RequestMapping("routine/pageQuery")
	@ResponseBody Object routinePageQuery(PointsQueryVO vo) {
		try {
			Page<StudentRoutineView> page = new Page<StudentRoutineView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<StudentRoutineView> list = studentRoutineService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("---routinePageQuery error={}", e);
		}
		return null;
	}
}
